﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class ReciveMessageDAO : BaseService
    {
        public IList<ReciveMessage> GetListPaging(int page, int pagesize, int userid, bool isDel, DateTime date1, DateTime date2, string key)
        {
            string hql = "from ReciveMessage  where 1=1";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and WorkerName like '%" + key + "%' or FromUserName like '%" + key + "%'";
            }
            if (date1 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate >'" + date1 + "'";
            }
            if (date2 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate <'" + date2 + "'";
            }
            hql += " and Worker =" + userid + " and IsDel =" + isDel + "  order By AddDate Desc";
            return FindList<ReciveMessage>(hql, null, (page - 1) * pagesize, pagesize);
        }

        public int GetListPaging(int userid, bool isDel, DateTime date1, DateTime date2, string key)
        {
            string hql = "select count(*) from ReciveMessage where 1=1";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and WorkerName like '%" + key + "%' or FromUserName like '%" + key + "%'";
            }
            if (date1 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate >'" + date1 + "'";
            }
            if (date2 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate <'" + date2 + "'";
            }
            hql += " and Worker =" + userid + " and IsDel =" + isDel;
            try
            {

                return Convert.ToInt32(GetValue(hql));

            }
            catch (Exception)
            {

                return 0;
            }
        }


        /// <summary>
        /// 根据发布信息用户和状态查询信件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList<ReciveMessage> GetListPaging(int page, int pagesize, int userid, int state, bool isread)
        {
            string hql = "from ReciveMessage where 1=1";
            if (state == -99)
            {
                hql += " and Worker = " + userid;
            }
            else
            {
                hql += " and Worker = " + userid + " and State =" + state;
            }
            hql += " and IsRead = " + isread + " order By AddDate Desc";
            return FindList<ReciveMessage>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 根据发布信息用户和状态查询任务信件条目数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, int state, bool isread)
        {
            string hql = "select count(*) from ReciveMessage where 1=1";
            if (state == -99)
            {
                hql += " and Worker = " + userid;
            }
            else
            {
                hql += " and Worker = " + userid + " and State =" + state + " and IsRead=" + isread;
            }
            return Convert.ToInt32(GetValue(hql));
        }
        public IList<ReciveMessage> GetListPaging(int page, int pagesize, int userid, int state)
        {
            string hql = "from ReciveMessage where 1=1";
            if (state == -99)
            {
                hql += " and Worker = " + userid;
            }
            else
            {
                hql += " and Worker = " + userid + " and State =" + state;
            }
            hql += " order By AddDate Desc";
            return FindList<ReciveMessage>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 根据发布信息用户和状态查询任务信件条目数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, int state)
        {
            string hql = "select count(*) from ReciveMessage where 1=1";
            if (state == -99)
            {
                hql += " and Worker = " + userid;
            }
            else
            {
                hql += " and Worker = " + userid + " and State =" + state;
            }
            return Convert.ToInt32(GetValue(hql));
        }


        /// <summary>
        /// 查询删除信件
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<ReciveMessage> GetListPaging(int userid, bool isDel, int page, int pagesize)
        {
            string hql = "from ReciveMessage where 1=1 and Worker=" + userid + " and  IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<ReciveMessage>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;

            }
        }

        /// <summary>
        /// 查询删除信件数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, bool isDel)
        {
            string hql = "select count(*) from ReciveMessage where 1=1 and Worker=" + userid + " and  IsDel = " + isDel;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRecieveMessageUpdate(string ids)
        {
            string hql = "update ReciveMessage set IsDel= true where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRRecieveMessageUpdate(string ids)
        {
            string hql = "update ReciveMessage set IsDel= false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRRecieveMessageDelete(string ids)
        {
            string hql = "delete ReciveMessage where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

    }
}
